package jt.classic.system.workflow;

import org.dom4j.Document;

import org.dom4j.io.SAXReader;

import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.RandomUtil;
import org.limp.mine.StringTool;
import org.limp.mine.XMLTool;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 绘制流转图形
 * @author iBizkit
 *
 */
public class PlotterServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.sendRedirect("test.jsp");
        doPost(request, response);
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String id = null;

        String type = "";
        
        String insid = null;
        
        String currentActId = null;

        try {
            CommonBean4HSQ cb = new CommonBean4HSQ(request, "UTF-8");

            Map param = cb.getResource();

            id = StringTool.checkString(param.get("id"));

            type = StringTool.checkString(param.get("type"));
            
            insid = StringTool.checkString(param.get("insid"));
            
            currentActId = StringTool.checkString(param.get("currentActId"));
            
            String instanceProcessXML = StringTool.checkString(param.get("instanceProcessXML"));

            String flowxml = StringTool.checkString(param.get("flowxml"));

            File file = new File(request.getRealPath("/") +
                    "XiorkFlow/processes/" + id + ".xml");

            XMLTool.writeXML(flowxml, file,
                XMLTool.defaultFormat(XMLTool.DEFAULT_ENCODING));
            
            if(!insid.equals("")){
            	File instanceFile = new File(request.getRealPath("/") +
                        "XiorkFlow/processes/" + insid + "activities.xml");

                XMLTool.writeXML(instanceProcessXML, instanceFile,
                    XMLTool.defaultFormat(XMLTool.DEFAULT_ENCODING));
            }

            request.setAttribute("flowxml", flowxml);
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }

        String url = null;

        if (type.equals("drawing")) {
            url = jt.classic.system.ISystem.getContextPath() +
                "/XiorkFlow/demo/updateprocess.jsp?name=" + id;
        } else if (type.equals("viewing")) {
            url = jt.classic.system.ISystem.getContextPath() +
                "/XiorkFlow/demo/viewprocess.jsp?name=" + id;
            
            if(!insid.equals("")){
            	url += "&insid=" + insid + "&currentActId=" + currentActId;
            }
        }

        response.sendRedirect(url);
    }
}
